package com.itetchologyandenglish.home.controller.Api;

import com.alibaba.fastjson.annotation.JSONField;
import com.github.pagehelper.PageInfo;
import com.itetchologyandenglish.home.config.Log;
import com.itetchologyandenglish.home.enetity.AuthUser;
import com.itetchologyandenglish.home.enetity.Orders;
import com.itetchologyandenglish.home.enetity.Result;
import com.itetchologyandenglish.home.service.CompanyService;
import com.itetchologyandenglish.home.service.OrdersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
@Api(tags={"国际采购模块"})
@RequestMapping("/orders")
@Controller
public class OrdersApiController {
    @Resource
    OrdersService ordersService;
    @Resource
    CompanyService companyService;
    @ApiOperation("查看国际采购列表")
    @Log("国际采购管理-查看列表")
    @ResponseBody
    @RequestMapping(value = "/findPurchaseOrderList", produces = "application/json")
    public Result<PageInfo> findPurchaseOrderList(Orders orders) {
        orders.setSupplyFlag(1);
        if (orders == null) {
            orders = new Orders();
        }
        PageInfo orderPageInfo = ordersService.findPurchaseOrderList(orders);
        System.out.println(orderPageInfo.toString());
        return new Result<PageInfo>(200, "success", orderPageInfo);
    }
    @ApiOperation("查看国际采购订单信息")
    @Log("国际采购管理-根据ID查看订单")
    @ResponseBody
    @RequestMapping(value = "/findPurchaseOrderByid", produces = "application/json")
    public Result<Orders> findPurchaseOrderByid(Integer orderId) {
        Orders orders = ordersService.findPurchaseOrderByid(orderId);
        return new Result<Orders>(200, "success", orders);
    }
    @ApiOperation("修改国际采购列表")
    @Log("国际采购管理-根据信息修改订单")
    @RequestMapping(value = "/updatePurchaseOrder", produces = "application/json")
    public String  updatePurchaseOrder(@RequestParam("files") CommonsMultipartFile[] files, Orders orders, HttpSession session) throws IOException {
        putFiles(files, orders, session);
        ordersService.updatePurchaseOrder(orders);
        return "redirect:/page/procurement";
    }
    @ApiOperation("删除国际采购列表")
    @Log("国际采购管理-根据ID删除订单")
    @RequestMapping(value = "/deletePurchaseOrder", produces = "application/json")
    public String  deletePurchaseOrder(Integer orderId) {
        ordersService.deletePurchaseOrder(orderId);
        return "redirect:/page/procurement";
    }
    public void putFiles( CommonsMultipartFile[] files, Orders orders, HttpSession session) throws IOException {
        orders.setCompanyid(companyService.findVipCompanyByCompanyName(orders.getCompanyName()).getCompanyId());
        AuthUser authUser = (AuthUser) session.getAttribute("user");
        orders.setOperatorId(authUser.getId());
        orders.setOperatorName(authUser.getUsername());
        String path = "C:\\lc_java_idea\\SSMBS\\src\\main\\webapp\\WEB-INF\\static\\images\\orders\\";
        File dir = new File(path);
        if (!dir.exists()) {
            dir.mkdir();
        }
        String[] fileName = new String[4];
        if (files != null && files.length > 0) {
            for (int i = 0; i < files.length; i++) {
                MultipartFile file = files[i];
                if(file.getOriginalFilename()!=null && !file.getOriginalFilename().equals("")) {
                    fileName[i] = UUID.randomUUID().toString() + "-" + file.getOriginalFilename();
                    System.out.println(file.getOriginalFilename());
                    file.transferTo(new File(path, fileName[i]));
                }
            }
            orders.setImg1(fileName[0]);
            orders.setImg2(fileName[1]);
            orders.setImg3(fileName[2]);
        }
    }
    @ApiOperation("添加国际采购列表")
    @Log("国际采购管理-根据信息添加订单")
    @JSONField(format = "yyyy-MM-dd") //返回时间类型
    @DateTimeFormat(pattern = "yyyy-MM-dd") //接收时间类型
    @RequestMapping(value = "/addPurchaseOrder",produces = "application/json")
    public String addPurchaseOrder(@RequestParam("files") CommonsMultipartFile[] files, Orders orders, HttpSession session) throws IOException {
        putFiles(files, orders, session);
        orders.setSupplyFlag(1);
        ordersService.addPurchaseOrder(orders);
        return "redirect:/page/procurement";
    }


}
